home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 52
/
Mac Magazin and MacEasy Magazine CD - Issue 52.iso
/
Updates
/
Stata 5.0 Ado-files
/
ado.sea
/
newado
/
table.ado
< prev
next >
Wrap
Text File
|
1998-11-22
|
6KB
|
320 lines
*! version 5.1.3 21may1998
program define table
version 5.0
local varlist "req ex max(3)"
local if "opt"
local in "opt"
local weight "fweight aweight pweight iweight"
local options "BY(string) COL CW Format(string) Name(string) REPLACE ROW SCol Contents(string) *"
parse "`*'"
local sctotal "`scol'"
local coltota "`col'"
local rowtota "`row'"
local stats "`content'"
local content
if "`by'" != "" {
unabbrev `by'
local by "$S_1"
local byopt "by(`by')"
}
parse "`varlist'", parse(" ")
local row "`1'"
local col "`2'"
local sc "`3'"
if "`coltota'"!="" & "`col'"=="" {
local coltota
}
if "`sctotal'"!="" & "`sc'"=="" {
local sctotal
}
if "`format'"=="" {
local format "%9.0g"
}
tempvar one touse
if "`stats'"=="" {
local stats "freq"
}
if "`replace'"!="" & "`name'"=="" {
local name "table"
}
local i 0
parse "`stats'", parse(" ")
while "`1'" != "" {
local i = `i' + 1
if "`replace'"!="" {
local res "`name'`i'"
}
else tempvar res
Parse "`weight'" `format' `res' `one' `*'
local clist "`clist' $S_1"
local cell "`cell' `res'"
local vlist "`vlist' $S_2"
local flist "`flist' $S_4"
mac shift $S_3
}
if `i'>5 /* limit from tabdisp */ {
di in red "too many stats()"
exit 103
}
confirm new var `res' /* in case replace */
quietly {
if "`weight'" != "" {
tempvar wvar
gen double `wvar' `exp'
local wgt "[`weight'=`wvar']"
}
mark `touse' `wgt' `if' `in'
count if `touse'
if _result(1)==0 {
noisily error 2000
}
preserve
keep if `touse'
drop `touse'
DropMis `varlist' `by'
if _N==0 {
noisily error 2000
}
gen byte `one' = 1
/*
if "`weight'"=="aweight" {
summarize `wvar', mean
replace `wvar' = `wvar'/_result(3)
local wgt "[iweight=`wvar']"
}
*/
keep `varlist' `by' `vlist' `wvar'
if "`rowtota'"=="" & "`coltota'"=="" & "`sctotal'"=="" {
collapse `clist' `wgt', by(`varlist' `by') fast `cw'
}
else {
tempfile res orig
save "`orig'"
collapse `clist' `wgt', by(`varlist' `by') fast `cw'
save "`res'"
if "`rowtota'" != "" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`col' `sc' `by'" "`cw'" "`wgt'"
}
if "`coltota'"!= "" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`row' `sc' `by'" "`cw'" "`wgt'"
if "`rowtota'" != "" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`sc' `by'" "`cw'" "`wgt'"
}
}
if "`sctotal'"!="" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`row' `col' `by'" "`cw'" "`wgt'"
if "`rowtota'" != "" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`col' `by'" "`cw'" "`wgt'"
}
if "`coltota'" != "" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`row' `by'" "`cw'" "`wgt'"
}
if "`rowtota'" != "" && "`coltota'"!="" {
AddRes "`res'" "`orig'" "`clist'" /*
*/ "`by'" "`cw'" "`wgt'"
}
}
}
}
/* set display formats */
parse "`flist'", parse(" ")
while "`1'" != "" {
format `1' `2'
mac shift 2
}
if "`c'"=="" | "`replace'"!="" {
FixLabs "(count) `one'" `cell'
}
if "`replace'"!="" {
mac drop S_FN S_FNDATE
restore, not
}
tabdisp `varlist', cell(`cell') `byopt' totals `options'
end
program define AddRes /* resfn origfn clist by cw wgt */
local res "`1'"
local orig "`2'"
local clist "`3'"
local by "`4'"
local cw "`5'"
local wgt "`6'"
use "`orig'", clear
local n : word count `by'
if `n' {
collapse `clist' `wgt', by(`by') fast `cw'
}
else collapse `clist' `wgt', fast `cw'
append using "`res'"
save "`res'", replace
end
program define Parse /* "weighttype" fmt newvar onevar stuff */
local weight "`1'"
local fmt "`2'"
local res "`3'"
local one "`4'"
mac shift 4
if "`1'"=="freq" {
global S_1 "(count) `res'=`one'"
global S_2 "`one'"
global S_3 1
if "`weight'"=="aweight" | "`weight'"=="iweight" | "`weight'"=="pweight" {
global S_4 "`res' `fmt'"
}
else global S_4 "`res' %9.0g"
exit
}
if "`2'"=="" {
di in red "`1' invalid or requires argument"
exit 198
}
/* synonyms */
if lower("`1'")=="n" {
local 1 "count"
}
else if substr("median",1,max(3,length("`1'")))=="`1'" {
local 1 "median"
}
else if substr("mean",1,length("`1'"))=="`1'" {
local 1 "mean"
}
unabbrev `2', max(1)
local vn "$S_1"
Valid `1' `vn' `fmt' `weight'
global S_4 "`res' $S_1"
global S_1 "(`1') `res'=`vn'"
global S_2 "`vn'"
global S_3 2
end
program define Valid /* word fromvar dfltfmt weighttype */
local w "`1'"
local v "`2'"
local f "`3'"
local weight "`4'"
if "`w'"=="sd" | "`w'"=="iqr" | "`w'"=="sum" | "`w'"=="rawsum" {
global S_1 "`f'" /* meaning default format */
exit
}
if "`w'"=="count" {
if "`weight'"=="aweight" | "`weight'"=="iweight" | "`weight'"=="pweight" {
global S_1 "`f'"
}
else global S_1 "%9.0g" /* meaning as-is format */
exit
}
/*
remaining have default format or
variable's date format
*/
local fmt : format `v'
if substr("`fmt'",1,2)=="%d" {
global S_1 "`fmt'"
}
else global S_1 "`f'"
if "`w'"=="mean" | "`w'"=="median" {
exit
}
if "`1'"=="max" | "`1'"=="min" {
exit
}
if substr("`1'",1,1)=="p" {
local n = substr("`1'",2,.)
capture confirm integer number `n'
if _rc==0 {
if `n'>0 & `n'<100 {
exit
}
}
}
di in red "`1' invalid"
exit 198
end
program define DropMis /* varnames */
while "`1'" != "" {
local t : type `1'
if substr("`t'",1,3)=="str" {
drop if `1'==""
}
else drop if `1'==.
mac shift
}
end
program define FixLabs /* lab varnames */
local true "`1'"
mac shift
while "`1'" != "" {
local lab : variable label `1'
if "`lab'"=="`true'" {
label var `1' "Freq."
}
else {
FixLab2 `1' `lab'
}
mac shift
}
end
program define FixLab2 /* label */
local var "`1'"
local wrd "`2'"
mac shift 2
if "`wrd'"=="(count)" {
label var `var' "N(`*')"
exit
}
if "`wrd'" == "(p" {
local wrd "`1'"
mac shift
if "`wrd'"=="50)" {
label var `var' "med(`*')"
}
else {
local wrd = substr("`wrd'",1,length("`wrd'")-1)
label var `var' "p`wrd'(`*')"
}
exit
}
if substr("`wrd'",1,1)=="(" & substr("`wrd'",-1,1)==")" {
local wrd = substr("`wrd'",2,length("`wrd'")-2)
label var `var' "`wrd'(`*')"
exit
}
end